package com.question.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.question.Dto.Content;
import com.question.Dto.QuestionDto;
import com.question.Exception.BaseException;
import com.question.Vo.ChoiceVo;
import com.question.Vo.QuestionImportVo;
import com.question.common.PageResult;
import com.question.entity.Paper;
import com.question.entity.Question;
import com.question.entity.QuestionChoice;
import com.question.mapper.PaperMapper;
import com.question.mapper.QuestionMapper;
import com.question.service.PaperService;
import com.question.service.QuestionChoiceService;
import com.question.service.QuestionService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private QuestionChoiceService questionChoiceService;

    @Transactional(rollbackFor = Exception.class)
    public void imports(List<QuestionImportVo> list) {
        for (QuestionImportVo questionImportVo : list) {
            LambdaQueryWrapper<Question> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Question::getName, questionImportVo.getContent());
            Question question = questionMapper.selectOne(lambdaQueryWrapper);
            //如果名称存在，不新增
            if (question != null) continue;
            Question question1 = new Question();
            question1.setCreatetime(new Date());
            question1.setName(questionImportVo.getContent());
            question1.setPaperid(questionImportVo.getPaperid());
            question1.setType(questionImportVo.getType());
            questionMapper.insert(question1);

            for (ChoiceVo choiceVo : questionImportVo.getChoiceVoList()) {
                QuestionChoice questionChoice = new QuestionChoice();
                questionChoice.setQuestionid(question1.getId());
                questionChoice.setCreatetime(new Date());
                questionChoice.setContent(choiceVo.getContent());
                questionChoice.setSort(choiceVo.getSort());
                questionChoiceService.save(questionChoice);
            }
        }
    }

    public PageResult selectByPage(Integer pageNo, Integer pageSize, String keyword) {
        PageResult pageResult = new PageResult();
        IPage<Question> page = new Page<>(pageNo, pageSize);
        IPage<Question> list = questionMapper.selectByPage(page, keyword);
        System.out.println(list);
        pageResult.setTotal(list.getTotal());
        pageResult.setRecords(list.getRecords());
        return pageResult;
    }

    @Transactional(rollbackFor = Exception.class)
    public void addquestion(QuestionDto questionDto) {
        LambdaQueryWrapper<Question> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Question::getPaperid, questionDto.getPaperid());
        lambdaQueryWrapper.eq(Question::getName, questionDto.getName());
        Question question = questionMapper.selectOne(lambdaQueryWrapper);
        if (question != null) {
            throw new BaseException("已存在该题目");
        }
        Question question1 = new Question();
        question1.setType(questionDto.getType());
        question1.setName(questionDto.getName());
        question1.setPaperid(questionDto.getPaperid());
        question1.setCreatetime(new Date());
        questionMapper.insert(question1);
        if (!questionDto.getType().equals(2)) {
            for (Content content : questionDto.getList()) {
                QuestionChoice questionChoice = new QuestionChoice();
                questionChoice.setSort(content.getSort());
                questionChoice.setQuestionid(question1.getId());
                questionChoice.setContent(content.getContent());
                questionChoice.setCreatetime(new Date());
                questionChoiceService.save(questionChoice);
            }
        }


    }

    @Transactional(rollbackFor = Exception.class)
    public void editQuestion(QuestionDto questionDto) {
        LambdaQueryWrapper<Question> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Question::getPaperid, questionDto.getPaperid());
        lambdaQueryWrapper.eq(Question::getName, questionDto.getName());
        lambdaQueryWrapper.notIn(Question::getId, questionDto.getId());
        Question question = questionMapper.selectOne(lambdaQueryWrapper);
        if (question != null) {
            throw new BaseException("该分类下存在同名题目");
        }
        Question question1 = new Question();
        BeanUtils.copyProperties(questionDto, question1);
        questionMapper.updateById(question1);
        if (!questionDto.getType().equals(2)) {
            LambdaQueryWrapper<QuestionChoice> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(QuestionChoice::getQuestionid, questionDto.getId());
            questionChoiceService.remove(lambdaQueryWrapper1);

            for (Content content : questionDto.getList()) {
                QuestionChoice questionChoice = new QuestionChoice();
                questionChoice.setSort(content.getSort());
                questionChoice.setQuestionid(questionDto.getId());
                questionChoice.setContent(content.getContent());
                questionChoice.setCreatetime(new Date());
                questionChoiceService.save(questionChoice);
            }
        }

    }

    public void deleteById(Integer id) {
        LambdaQueryWrapper<Question> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(Question::getId, id);
        Question question = questionMapper.selectOne(lambdaQueryWrapper);
        if (question == null) {
            throw new BaseException("该题目不存在");
        }
        questionMapper.deleteById(id);
        if (!question.getType().equals(2)) {
            LambdaQueryWrapper<QuestionChoice> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(QuestionChoice::getQuestionid,id);
            questionChoiceService.remove(lambdaQueryWrapper1);
        }


    }

}
